﻿using System;
using Microsoft.AspNetCore.Mvc;

namespace API接口示范
{
    [Route("api/Controller")]
    [ApiController]
        public class ControllerController : ControllerBase
        {
            public static Func<int, Querydata> GetTextBoxValue1;
            public static Func<string, bool> Control;
        /// <summary>
        /// 获取自定义类型的方法
        /// </summary>
        /// <param name="a">需要获取的类别代表参数，1.设备日志;2.设备状态;3.灯</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Getquery(int a)//返回自定义的类型
        {
            try
            {

                Querydata text = GetTextBoxValue1?.Invoke(a);
                if (text == null)
                {
                    // 这里可以创建一个默认的 数据 对象，而不是使用字符串 "未初始化"
                    text = new Querydata { qdata = "错误", content = "未初始化" };
                }

                return Ok(new { Text = text });

            }
            catch (Exception ex)
            {
                return StatusCode(500, ex.Message);
            }
        }

        /// <summary>
        /// 控制界面按键
        /// </summary>
        /// <param name="a">界面按键参数。1.急停;2.启动;3.停止;4.复位;</param>
        /// <returns></returns>
           [HttpPut("getdata")]
            public IActionResult Controlmethod(string a)//控制类，返回是否成功
            {
                try
                {

                    bool result = Control?.Invoke(a) ?? false;
                    return Ok(result);
                }
                catch (Exception ex)
                {
                    return StatusCode(500, $"发生内部错误: {ex.Message}");
                }

            }

        }
    

    public class Querydata//自定义类型
    {
        public string qdata { get; set; }
        public string? content {  get; set; }
        public int? status {  get; set; }
    }
}
